package com.czf.testmodele.controller;

import cn.hutool.core.map.MapUtil;
import com.czf.testmodele.service.ParamService;
import com.czf.testmodele.service.UpLoadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * @author zfChen
 * @create 2022/3/10 13:24
 */
@RestController
@Slf4j
public class UpLoadController {

    @Autowired
    private UpLoadService upLoadService;

    @Autowired
    private ParamService paramService;

    /**
     * 扫描文件，将insert语句转化为select语句，查询数据库进行验证
     * @param sqlFile
     * @return
     */
    @RequestMapping("/transformToSelect")
    public List<String> transformToSelect(@RequestPart("sqlFile") MultipartFile sqlFile) {
        List<String> list = new ArrayList<>();
        HashMap<Object, Object> map = new HashMap<>();
        //从文件中获取insert语句
        List<String> sqls = upLoadService.getSql(sqlFile);

        for (int i = 0; i < sqls.size(); i++) {
            // 获得字段和值的map
            HashMap<String, Object> insertMap = upLoadService.getInsertMap(sqls.get(i));
            // 得到拼接好的select语句并查询
            Map<String, Object> sqlMap = upLoadService.splitSql(insertMap);
            if (MapUtil.isEmpty(sqlMap)) {
                log.info("出现问题的sql={}",sqls.get(i));
                list.add(sqls.get(i));
                continue;
            }
        }
        return list;
    }

}
